package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.pojo.entity.User;
import com.example.service.IUserService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Service
@Slf4j
@DS("master")
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(User user) {
        user.setIsDeleted(0);
        user.setVersion(0);
        userMapper.insert(user);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        User user = userMapper.selectById(id);
        user.setIsDeleted(1);
        updateById(user);
    }


    @Override
    public Page<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        MPJLambdaWrapper<User> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(User.class)
                .eq(User::getIsDeleted,0)
                .eq(ObjectUtil.isNotNull(user.getName()),User::getName,user.getName());
        return page(new Page<>(pageNum, pageSize), mpjLambdaWrapper);
    }

}
